package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.theme.common.base.GenerateOrderNumUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.constant.DownloadTypeEnum;
import com.theme.passport.entity.constant.UserPayOrderTypeEnum;
import com.theme.passport.entity.theme.*;
import com.theme.passport.form.ApplePayElementByAdForm;
import com.theme.passport.form.ApplePayElementForm;
import com.theme.passport.form.ApplePayVipForm;
import com.theme.passport.service.UserService;
import com.theme.passport.service.theme.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

//@Transactional
@Service("applePayService")
public class AppleServiceImpl implements ApplePayService {

    public static Logger logger = LoggerFactory.getLogger(AppleServiceImpl.class);

    @Autowired
    private UserService userService;

    @Autowired
    private UserPayOrderService userPayOrderService;

    @Autowired
    private UserBuyService userBuyService;

    @Autowired
    private WallpaperService wallpaperService;

    @Autowired
    private ThemeService themeService;

    @Autowired
    private WidgeService widgeService;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public R appleBuyVip(ApplePayVipForm applePayForm) {
        //app端所提供的收据是有效的===>验证成功
        // 1、入库交易记录生成订单
        UserPayOrderEntity userPayOrder = new UserPayOrderEntity();
        userPayOrder.setType(UserPayOrderTypeEnum.VIP.getValue());
        userPayOrder.setUserId(applePayForm.getUserId());
        userPayOrder.setPrice(applePayForm.getPrice());
        userPayOrder.setOrderNo(GenerateOrderNumUtils.generateOrderNum());
        userPayOrder.setMonths(applePayForm.getMonths());

        userPayOrder.setProductId(applePayForm.getProductId());
        userPayOrder.setPurchaseDate(applePayForm.getPurchaseDate());
        userPayOrder.setBundleId(applePayForm.getBundleId());
        userPayOrder.setApplicationVersion(applePayForm.getApplicationVersion());
        userPayOrder.setTransactionId(applePayForm.getTransactionId());
        userPayOrder.setStatus(1);
        userPayOrder.setCreated(new Date());
        userPayOrderService.save(userPayOrder);

        //2、修改用户vip 和vip过期时间
        UserEntity user = userService.updateUserVipMonths(applePayForm.getUserId(), applePayForm.getMonths());
        user.setPassword(null);

        return R.ok("购买VIP成功!").put("user", user);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public R appleBuyElement(ApplePayElementForm applePayForm) {
        //app端所提供的收据是有效的===>验证成功
        //===================处理自己的业务============================
        String orderNum = GenerateOrderNumUtils.generateOrderNum();
        // 1、入库交易记录生成订单
        UserPayOrderEntity userPayOrder = new UserPayOrderEntity();
        userPayOrder.setOrderNo(orderNum);
        userPayOrder.setUserId(applePayForm.getUserId());
        userPayOrder.setPrice(applePayForm.getPrice());
        userPayOrder.setType(applePayForm.getType());
        userPayOrder.setStatus(1);
        userPayOrder.setCreated(new Date());

        userPayOrder.setProductId(applePayForm.getProductId());
        userPayOrder.setPurchaseDate(applePayForm.getPurchaseDate());
        userPayOrder.setBundleId(applePayForm.getBundleId());
        userPayOrder.setApplicationVersion(applePayForm.getApplicationVersion());
        userPayOrder.setTransactionId(applePayForm.getTransactionId());
        userPayOrderService.save(userPayOrder);

        // 2、入库记录生成订单
        UserBuyEntity userBuy = new UserBuyEntity();
        userBuy.setOrderNo(orderNum);
        userBuy.setUserId(applePayForm.getUserId());
        userBuy.setPrice(applePayForm.getPrice());
        userBuy.setType(applePayForm.getType());
        userBuy.setObjectId(applePayForm.getObjectId());
        userBuy.setCreated(new Date());

        QueryWrapper<UserBuyEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper
                .eq("user_id", applePayForm.getUserId())
                .eq("type", userBuy.getType())
                .eq("object_id", userBuy.getObjectId()));
        if (userBuy.getType() == DownloadTypeEnum.THEME.getValue()){
            ThemeEntity theme = themeService.getById(userBuy.getObjectId());
            if (theme != null){
                userBuy.setObjectName(theme.getName());
                userBuy.setImage(theme.getThumb());
            }
        }else if (userBuy.getType() == DownloadTypeEnum.WIDGE.getValue()){
            WidgeEntity widge = widgeService.getById(userBuy.getObjectId());
            if (widge != null){
                userBuy.setObjectName(widge.getName());
                userBuy.setImage(widge.getPreview());
            }
        }else {
            WallpaperEntity wallpaper = wallpaperService.getById(userBuy.getObjectId());
            if (wallpaper != null){
                userBuy.setObjectName(wallpaper.getName());
                userBuy.setImage(wallpaper.getImage());
            }
        }
        userBuyService.save(userBuy);
        return R.ok("购买成功!").put("userBuy",userBuy);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public R appleBuyElementByAd(ApplePayElementByAdForm applePayForm) {
        //app端所提供的收据是有效的===>验证成功
        //===================处理自己的业务============================
        String orderNum = GenerateOrderNumUtils.generateOrderNum();
        // 2、入库记录生成订单
        UserBuyEntity userBuy = new UserBuyEntity();
        userBuy.setOrderNo(orderNum);
        userBuy.setUserId(applePayForm.getUserId());
        userBuy.setPrice(applePayForm.getPrice());
        userBuy.setType(applePayForm.getType());
        userBuy.setObjectId(applePayForm.getObjectId());
        userBuy.setCreated(new Date());

        QueryWrapper<UserBuyEntity> orderWrapper = new QueryWrapper<>();
        orderWrapper.and(wrapper -> wrapper
                .eq("user_id", applePayForm.getUserId())
                .eq("type", userBuy.getType())
                .eq("object_id", userBuy.getObjectId()));
        if (userBuy.getType() == DownloadTypeEnum.THEME.getValue()){
            ThemeEntity theme = themeService.getById(userBuy.getObjectId());
            if (theme != null){
                userBuy.setObjectName(theme.getName());
                userBuy.setImage(theme.getThumb());
            }
        }else if (userBuy.getType() == DownloadTypeEnum.WIDGE.getValue()){
            WidgeEntity widge = widgeService.getById(userBuy.getObjectId());
            if (widge != null){
                userBuy.setObjectName(widge.getName());
                userBuy.setImage(widge.getPreview());
            }
        }else {
            WallpaperEntity wallpaper = wallpaperService.getById(userBuy.getObjectId());
            if (wallpaper != null){
                userBuy.setObjectName(wallpaper.getName());
                userBuy.setImage(wallpaper.getImage());
            }
        }
        userBuyService.save(userBuy);
        return R.ok("购买成功!").put("userBuy",userBuy);
    }


}

